package com.example.cet4try.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;


import android.os.Bundle;
import android.view.MenuItem;

import com.example.cet4try.MyFragment.MyFragment1;
import com.example.cet4try.MyFragment.MyFragment2;
import com.example.cet4try.MyFragment.MyFragment3;
import com.example.cet4try.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<Fragment>fragments;
    private BottomNavigationView bottomNavigationView;
    private MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        viewPager = findViewById(R.id.viewpager1);
        bottomNavigationView = findViewById(R.id.bottom1);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.item1:
                        viewPager.setCurrentItem(0);break;
                    case R.id.item2:
                        viewPager.setCurrentItem(1);break;
                    case R.id.item3:
                        viewPager.setCurrentItem(2);break;
                    /*case R.id.item4:
                        viewPager.setCurrentItem(3);break;*/
                    default:
                        break;

                }
                return true;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem!=null){
                    menuItem.setChecked(false);
                }
                else {
                    menuItem = bottomNavigationView.getMenu().getItem(0);
                    menuItem.setChecked(true);
                }
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        fragments = new ArrayList<>();
        fragments.add(new MyFragment1());
        fragments.add(new MyFragment2());
        fragments.add(new MyFragment3());
        FragmentAdapter myadapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(myadapter);

    }

    private class FragmentAdapter extends FragmentStatePagerAdapter{
        private List<Fragment> list;
        public FragmentAdapter(@NonNull FragmentManager fm,List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
